package com.happymmall.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.happymmall.service.UserService;
import com.happymmall.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/check_valid.do")
public class CheckValidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setContentType("application/json;charset=utf-8");

        // 获取参数
        String str = req.getParameter("str");
        String type = req.getParameter("type");

        // 调用业务逻辑
        UserService userService = new UserServiceImpl();
        boolean rs = userService.checkvalid(str, type);

        // 返回结果
        if (rs) {

            resp.setContentType("application/json;charset=utf-8");
            Map<String, Object> resultData = new HashMap();
            resultData.put("status", 1);
            resultData.put("msg", "用户已存在");

            JSONObject jsonObject = new JSONObject();
            String info = jsonObject.toJSONString(resultData);
            PrintWriter out = resp.getWriter();
            out.write(info);
        } else {
            resp.setContentType("application/json;charset=utf-8");
            Map<String, Object> resultData = new HashMap();
            resultData.put("status", 0);
            resultData.put("msg", "校验成功");

            JSONObject jsonObject = new JSONObject();
            String info = jsonObject.toJSONString(resultData);
            PrintWriter out = resp.getWriter();
            out.write(info);
        }
    }


}
